import React from 'react';
import PropTypes from 'prop-types';
import WordSvg from './images/word.svg';
import ExcelSvg from './images/excel.svg';
import _ from 'lodash';
import {
  getZoomeyeExportList,
  downloadZoomeyeExport,
  deleteZoomeyeExport,
  exportZoomeyeExport,
  getOnedayExportList,
  downloadOnedayExport,
  deleteOnedayExport,
  exportOnedayExport,
  getWebExportList,
  downloadWebExport,
  deleteWebExport,
  exportWebExport,

  // 1day报告相关接口
  getOnedayTaskReportList,
  downloadOnedayTaskReport,
  deleteOnedayTaskReport,
  exportOnedayTaskReport,

  // Zoomeye探测报告相关接口
  getZoomeyeTaskReportList,
  downloadZoomeyeTaskReport,
  deleteZoomeyeTaskReport,
  exportZoomeyeTaskReport,

  // Web指纹探测报告相关接口
  getWebTaskReportList,
  downloadWebTaskReport,
  deleteWebTaskReport,
  exportWebTaskReport,

  // 漏洞普查报告报表相关接口
  exportVulDetectReport,
  showVulDetectReport,
  downloadVulDetectTaskReport,
  deleteVulDetectTaskReport,
  exportVulDetectTaskXlsx,
  showVulDetectTaskXlsx,
  downloadVulDetectTaskXlsx,
  deleteVulDetectTaskXlsx
} from '@api/task';
import ExportModal from '@components/export-modal';
import './index.less';

const TaskExportModal = props => {
  const { task = {}, visible, onVisibleChange, isDetail } = props;
  const { _id, scanType } = task;
  const isWeakPassTask = _.get(task, 'onedayTaskParams.isWeakPassTask', false);
  const combineListLoader = (...requesters) => {
    return (...params) => {
      let requests = requesters.map(requester => requester(...params));
      return Promise.all(requests).then(results => {
        let files = results.reduce((total, current, index) => {
          let currentFiles = _.get(current, 'data.data.files');
          currentFiles.forEach(currentFile => (currentFile._requestIndex = index));
          return total.concat(currentFiles);
        }, []);
        files.sort((a, b) => {
          if (_.get(a, 'created', '') < _.get(b, 'created', '')) {
            return 1;
          } else {
            return -1;
          }
        });
        return { data: { data: { files: files.slice(0, 7) } } };
      });
    };
  };

  const chooseOperator = ({ item, docxOperator, xlsxOperator }) => {
    return (...params) => {
      if (item._requestIndex === 0) {
        return docxOperator(...params);
      } else {
        return xlsxOperator(...params);
      }
    };
  };

  const postDownloadDispatcher = (dispatcher, requestIndex) => (...params) => {
    return dispatcher(...params).then(res => {
      const files = _.get(res, 'data.data.files', []);
      files.forEach(file => (file._requestIndex = requestIndex));
      return res;
    });
  };

  /* eslint-disable */
  const commonOperators = {
    Zoomeye: {
      listLoader: () => combineListLoader(getZoomeyeTaskReportList, getZoomeyeExportList)(_id),
      downloader: (filename, item) => {
        return chooseOperator({
          item,
          docxOperator: downloadZoomeyeTaskReport,
          xlsxOperator: downloadZoomeyeExport
        })({
          fileName: filename,
          taskId: _id
        });
      },
      deleter: (filename, item) => {
        return chooseOperator({
          item,
          docxOperator: deleteZoomeyeTaskReport,
          xlsxOperator: deleteZoomeyeExport
        })({
          fileName: filename,
          taskId: _id
        });
      }
    },
    OneDay: {
      listLoader: () => combineListLoader(getOnedayTaskReportList, getOnedayExportList)(_id),
      downloader: (filename, item) => {
        return chooseOperator({
          item,
          docxOperator: downloadOnedayTaskReport,
          xlsxOperator: downloadOnedayExport
        })({
          filename,
          task_id: _id
        });
      },
      deleter: (filename, item) => {
        return chooseOperator({
          item,
          docxOperator: deleteOnedayTaskReport,
          xlsxOperator: deleteOnedayExport
        })({
          filename,
          task_id: _id
        });
      }
    },
    Web: {
      listLoader: () => combineListLoader(getWebTaskReportList, getWebExportList)(_id),
      downloader: (filename, item) => {
        return chooseOperator({
          item,
          docxOperator: downloadWebTaskReport,
          xlsxOperator: downloadWebExport
        })({
          fileName: filename,
          taskId: _id
        });
      },
      deleter: (filename, item) => {
        return chooseOperator({
          item,
          docxOperator: deleteWebTaskReport,
          xlsxOperator: deleteWebExport
        })({
          fileName: filename,
          taskId: _id
        });
      }
    },
    VulDetect: {
      listLoader: () => combineListLoader(showVulDetectReport, showVulDetectTaskXlsx)(_id),
      downloader: (filename, item) => {
        return chooseOperator({
          item,
          docxOperator: downloadVulDetectTaskReport,
          xlsxOperator: downloadVulDetectTaskXlsx
        })({
          filename: filename,
          taskId: _id
        });
      },
      deleter: (filename, item) => {
        return chooseOperator({
          item,
          docxOperator: deleteVulDetectTaskReport,
          xlsxOperator: deleteVulDetectTaskXlsx
        })({
          filename: filename,
          taskId: _id
        });
      }
    }
  };

  const exportModalConfigMap = {
    Zoomeye: [
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={WordSvg} />
            Word
          </span>
        ),
        target: '资产探测任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportZoomeyeTaskReport({ ...filter, taskId: _id }),
          0
        ),
        ...commonOperators.Zoomeye
      },
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={ExcelSvg} />
            Excel
          </span>
        ),
        target: '资产探测任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportZoomeyeExport({ ...filter, taskId: _id }),
          1
        ),
        ...commonOperators.Zoomeye
      }
    ],
    OneDay: [
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={WordSvg} />
            Word
          </span>
        ),
        target: isWeakPassTask ? '弱口令任务报告' : '1DAY任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportOnedayTaskReport({ ...filter, task_id: _id }),
          0
        ),
        ...commonOperators.OneDay
      },
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={ExcelSvg} />
            Excel
          </span>
        ),
        target: isWeakPassTask ? '弱口令任务报告' : '1DAY任务报告',
        enableRangePick: true,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportOnedayExport({ ...filter, task_id: _id }),
          1
        ),
        ...commonOperators.OneDay
      }
    ],
    Web: [
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={WordSvg} />
            Word
          </span>
        ),
        target: 'Web指纹探测任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportWebTaskReport({ ...filter, taskId: _id }),
          0
        ),
        ...commonOperators.Web
      },
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={ExcelSvg} />
            Excel
          </span>
        ),
        target: 'Web指纹探测任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportWebExport({ ...filter, taskId: _id }),
          1
        ),
        ...commonOperators.Web
      }
    ],
    Honeypot: [
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={WordSvg} />
            Word
          </span>
        ),
        target: '蜜罐探测任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportWebTaskReport({ ...filter, taskId: _id }),
          0
        ),
        ...commonOperators.Web
      },
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={ExcelSvg} />
            Excel
          </span>
        ),
        target: '蜜罐探测任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportWebExport({ ...filter, taskId: _id }),
          1
        ),
        ...commonOperators.Web
      }
    ],
    VulDetect: [
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={WordSvg} />
            Word
          </span>
        ),
        target: '漏洞普查任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportVulDetectReport({ taskId: _id }),
          0
        ),
        ...commonOperators.VulDetect
      },
      {
        key: (
          <span className="tmd-multi-select-key">
            <img src={ExcelSvg} />
            Excel
          </span>
        ),
        target: '漏洞普查任务报告',
        enableRangePick: false,
        downloadDispatcher: postDownloadDispatcher(
          filter => exportVulDetectTaskXlsx({ ...filter, taskId: _id }),
          1
        ),
        ...commonOperators.VulDetect
      }
    ]
  };
  const exportModalConfig = exportModalConfigMap[`${scanType}`] || {};
  return (
    <ExportModal
      visible={visible}
      onVisibleChange={onVisibleChange}
      multiConfigs={exportModalConfig}
      isDetail={isDetail}
    />
  );
};

TaskExportModal.propTypes = {
  task: PropTypes.object,
  visible: PropTypes.string,
  onVisibleChange: PropTypes.func,
  isDetail: PropTypes.bool
};

export default TaskExportModal;
